<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2007 The Cacti Group                                      |
 |                                                                         |
 | This program is free software; you can redistribute it and/or           |
 | modify it under the terms of the GNU General Public License             |
 | as published by the Free Software Foundation; either version 2          |
 | of the License, or (at your option) any later version.                  |
 |                                                                         |
 | This program is distributed in the hope that it will be useful,         |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
 | GNU General Public License for more details.                            |
 +-------------------------------------------------------------------------+
 | Cacti: The Complete RRDTool-based Graphing Solution                     |
 +-------------------------------------------------------------------------+
 | This code is designed, written, and maintained by the Cacti Group. See  |
 | about.php and/or the AUTHORS file for specific developer information.   |
 +-------------------------------------------------------------------------+
 | http://www.cacti.net/                                                   |
 +-------------------------------------------------------------------------+
*/

$guest_account = true;
chdir('../../');
include("./include/auth.php");
include_once (dirname(__FILE__) . "/model/nms_system_model.php");
//define("MAX_DISPLAY_PAGES", 21);
//Nhan su kien tu client gui len
if (isset($_REQUEST['run'])){
	$linkchoice=$_REQUEST['run'];	
}else{
	$linkchoice='';
}
$check_alarm =system_set_access();
$check_setting = system_set_access_1();

//Kiem tra cac su kien truoc khi load trang
switch($linkchoice){
	/*
	 * Function manage alarm 
	 * */
	case 'alarm' :
		if(isset($_REQUEST['export'])){
			if($_REQUEST['export']=="show"){
				header("Content-type: application/csv");
				header("Content-Disposition: attachment; filename=interface_change_status.csv");
				print "Host,Interface,Status,Time,User_active\n";
				$interface_id = $_REQUEST['id_show'];
				$result_show =db_fetch_assoc("SELECT CONCAT('(',a.interface_name,')','\t',a.interface_alias) as interface_name ,CONCAT_WS('',c.description,' (',c.hostname,')') as host_name,
												b.nms_status as nms_status ,b.nms_time as nms_time,b.user_active as user_active
												From plugin_nms_interface a,plugin_nms_log b,host c WHERE a.interface_id = b.interface_id 
												and a.host_id =c.id and a.interface_id ='$interface_id' order by a.interface_id ");
				foreach ($result_show as $set){
					$interface_name =  '"'.$set['interface_name'].'"';
					print $set['host_name'] . ",";
					print $interface_name . ",";
					print $set['nms_status']. ",";
					print $set['nms_time']. ",";
					print $set['user_active'] . "\n";
				}
				exit;
			}
		}
			include_once (dirname(__FILE__) . "/nms_alarm_view.php");
			include_once (dirname(__FILE__) . "/libs/general_header.php");
			if($check_alarm==true){
				draw_alarm_view();
			include_once (dirname(__FILE__) . "/controller/nms_alarm_controller.php");
			}else{
			include_once (dirname(__FILE__) . "/libs/auth.php");
			}
			include_once($config['base_path'] . "/include/bottom_footer.php");
			break;
	/*
	 * Funcition manage log device and interface
	 */
	case 'log' :
			include_once (dirname(__FILE__) . "/libs/general_header.php");
			if($check_alarm==true){
				include_once (dirname(__FILE__) . "/controller/nms_history_controller.php");
				draw_log_all_view();
			}else{
				include_once (dirname(__FILE__) . "/libs/auth.php");
			}
			include_once($config['base_path'] . "/include/bottom_footer.php");
			break;
	/*
	 * Function manage setting NMS
	 * */
	case 'setting' :
			include_once (dirname(__FILE__) . "/nms_setting_view.php");
			include_once (dirname(__FILE__) . "/libs/general_header.php");
			if($check_setting==true){
				include_once (dirname(__FILE__) . "/controller/nms_setting_controller.php");
				draw_setting();
			}else{
				include_once (dirname(__FILE__) . "/libs/auth.php");
			}
			include_once($config['base_path'] . "/include/bottom_footer.php");
			break;
	/*
	 * Function manage interface management
	 * */
	case 'group' :
			include_once (dirname(__FILE__) . "/libs/general_header.php");
			if($check_setting==true){
				include_once (dirname(__FILE__) . "/controller/nms_sms_group_controller.php");
				draw_interface_management();
			}else{
				include_once (dirname(__FILE__) . "/libs/auth.php");
			}
			include_once($config['base_path'] . "/include/bottom_footer.php");
			break;
	/*
	 * Function manage device management
	 * */		
	case 'dim' :
		/*
		 * Report device information
		 * */
		if(isset($_REQUEST['export'])){
			if($_REQUEST['export']=="dim"){
				if (empty($_REQUEST['host_id'])){
					echo'<script>
								alert("Please choose devices !!!");
								</script>	';
				}elseif(empty($_REQUEST['select'])){
					echo'<script>
								alert("Please choose select report !!!");
								</script>	';
				}else{
				$host_id = $_REQUEST['host_id'];
				$date = $_REQUEST['date'];
				if($_REQUEST['select']==1){
					header("Content-type: application/csv");
					header("Content-Disposition: attachment; filename=device_module_report.csv");
					print "Host,Module,Module_serial,Slot_cases,Port_used,Port_notused,Software,Firmware,Sub_module,Sub_module_serial\n";
					$result_dim = db_fetch_assoc("SELECT a.module_name as module_name, a.hardware as module_hardware , a.firmware as module_firware , a.module_serial as module_serial ,
								 b.software as module_software ,b.module_num as module_num ,c.sub_name as sub_name ,c.sub_serial as sub_serial,CONCAT_WS('',d.description,' (',d.hostname,')') as host_name 
								from plugin_nms_module a, plugin_nms_modulelog b ,plugin_nms_submodule c,host d
								where a.module_serial= b.module_serial and a.module_serial = c.module_serial and b.host_id =d.id and b.host_id =".$host_id." and b.module_date ='$date'
					 order by host_id,module_num,module_name");
					foreach ($result_dim as $item){
						include_once(dirname(__FILE__) . "/model/nms_dim_model.php");
						$port_used = get_sum_port_used($host_id, $date,$item['module_num']);
						$port_empty= get_sum_port_empty($host_id, $date,$item['module_num']);
						$port_notused = get_sum_port_notuse($host_id,$date,$item['module_num']);
						$post_notused= $port_empty + $port_notused;
						print $item['host_name'] . ",";
						print $item['module_name']. ",";
						print $item['module_serial']. ",";
						print $item['module_num'] . ",";
						print $port_used . ",";
						print $post_notused . ",";
						print $item['module_software'] . ",";
						print $item['module_firware'] . ",";	
						print $item['sub_name'] . ",";
						print $item['sub_serial'] . "\n";
					}
				}elseif ($_REQUEST['select']==2){
					header("Content-type: application/csv");
					header("Content-Disposition: attachment; filename=device_module_report.csv");
					print "Host,Module,Module_serial,Slot cases,Interface Name,Rvalue,Thold,Status,Transceiver,Transceiver_serial\n";
					$result_dim = db_fetch_assoc("SELECT a.module_name as module_name, a.hardware as module_hardware , a.firmware as module_firware , a.module_serial as module_serial ,
								 b.software as module_software ,b.module_num as module_num ,c.sub_name as sub_name ,c.sub_serial as sub_serial,CONCAT_WS('',d.description,' (',d.hostname,')') as host_name 
								from plugin_nms_module a, plugin_nms_modulelog b ,plugin_nms_submodule c,host d
								where a.module_serial= b.module_serial and a.module_serial = c.module_serial and b.host_id =d.id and b.host_id =".$host_id." and b.module_date ='$date'
					group by module_num order by host_id,module_num,module_name");
					foreach ($result_dim as $item){
						$result =db_fetch_assoc("SELECT CONCAT('(',port_name,')','\t',port_alias) as interface_name,port_pause, port_status ,trans_name,rvalue,thold , trans_serial
																			from plugin_nms_translog
																			where host_id =".$host_id." and trans_date ='$date' and SUBSTRING(port_name, 3,1) = ".$item['module_num']."
																			Order by Convert(SUBSTRING(port_name,5,2),UNSIGNED INTEGER)");
						
						foreach ($result as $set){
							$interface_name =  '"'.$set['interface_name'].'"';
							switch ($set['thold']){
								case '1': $thold = '++';
								break;
								case '2': $thold = '+';
								break;
								case '3': $thold = '-';
								break;
								case '4': $thold = '--';
								break;
								case '0': $thold = '';
								break;
									
							}
							print $item['host_name'] . ",";
							print $item['module_name']. ",";
							print $item['module_serial']. ",";
							print $item['module_num'] . ",";
							print $interface_name . ",";
							print $set['rvalue']. ",";
							print $thold . ",";
							print $set['port_status'] . ",";
							print $set['trans_name'] . ",";
							print $set['trans_serial']  . "\n";
						}
					}
				}
				exit;
				}
			}
		}
			include_once (dirname(__FILE__) ."/nms_dim_view.php");
			include_once (dirname(__FILE__) . "/libs/general_header.php");			
			if($check_alarm==true){
				draw_dim();	
			}else{
				include_once (dirname(__FILE__) . "/libs/auth.php");
			}		
			include_once($config['base_path'] . "/include/bottom_footer.php");
			break;
	/*
	 * Function report alarm 
	 */
	case 'report_alarm' :
			if(isset($_REQUEST['export'])){
				if($_REQUEST['export']=='interface'){
					if(empty($_REQUEST['shost'])){
						echo'<script>
								alert("Please choose device !!!");
								</script>	';
					}elseif (empty($_REQUEST['sgroup'])){
						echo'<script>
								alert("Please choose group interface !!!");
								</script>	';
					}elseif (empty($_REQUEST['limit']) and empty($_REQUEST['top'])){
						echo'<script>
								alert("Please choose limit or top changes !!!");
								</script>	';
					}else{
					$datefrom =$_REQUEST['datefrom'];
					$dateto =$_REQUEST['dateto'];
					$where ="";
					$limit = $_REQUEST['limit'];
					if($limit==""){
						$limit=1;
					}
						switch ($_REQUEST['top']){
							case '1': $where =" limit 0,10 ";
							break;
							case '2': $where =" limit 0,20 ";
							break;
							case '3': $where =" limit 0,30 ";
							break;
							case '4': $where =" limit 0,40 ";
							break;
							case '5': $where =" limit 0,50 ";
							break;
							case '6': $where =" limit 0,100 ";
							break;
						}
						$where1 ="";
					if($_REQUEST['shost']=='all'&& $_REQUEST['sgroup']=='all_group'){
							$where1 ="";
					}elseif ($_REQUEST['shost']=='select'&& $_REQUEST['sgroup']=='all_group'){
							$result_host = db_fetch_assoc('SELECT host_id FROM plugin_nms_report_host where report_group="interface_report" ');
							$count =0 ;
							foreach ($result_host as $item){
								if($count == 0){
									$where1 = ' b.host_id ='.$item['host_id'];
								}else{
									$where1 = $where1.' or  b.host_id ='.$item['host_id'] . ' ';
								}
								$count++;
							}
					}elseif($_REQUEST['shost']=='all'&& $_REQUEST['sgroup']=='select_group'){
							$result_group = db_fetch_assoc('SELECT interface_group_id FROM plugin_nms_report_group where report_group= "interface_report" ');
							$count =0 ;
							foreach ($result_group as $item){
								if($count == 0){
									$where1 = '( a.interface_group_id ='.$item['interface_group_id'];
								}else{
									$where1 = $where1.' or  a.interface_group_id ='.$item['interface_group_id'] . ' ';
								}
								$count++;
							}
							$where1 = $where1.') ';
					}elseif ($_REQUEST['shost']=='select'&& $_REQUEST['sgroup']=='select_group'){
							$result_host_group = db_fetch_assoc('SELECT host_id , interface_group_id FROM plugin_nms_report_host a ,plugin_nms_report_group b where a.report_group ="interface_report" and a.report_group =b.report_group group by host_id,interface_group_id');
							$count =0 ;
							foreach ($result_host_group as $item){
								if($count == 0){
									$where1 = '(( b.host_id='.$item['host_id'].' and a.interface_group_id ='.$item['interface_group_id'].') ';
								}else{
								$where1 = $where1.'or ( b.host_id='.$item['host_id'].' and a.interface_group_id ='.$item['interface_group_id']. ')  ';
								}
								$count++;
							}
							$where1 = $where1.') ';
					}
					if(!empty($where1)){
						$where1=$where1.' and ';
					}
					header("Content-type: application/csv");
					header("Content-Disposition: attachment; filename=interface_change_status_results.csv");
					print "Interface,Hostname,Status,TimeChange,User_active\n";
					$data_report_interface = db_fetch_assoc("Select a.nms_status as nms_status , a.nms_time  as nms_time , CONCAT('(',b.interface_name,')','\t',b.interface_alias) as interface_name ,CONCAT_WS('',d.description,' (',d.hostname,')') as host_name ,a.user_active as user_active, num
							FROM plugin_nms_log a,plugin_nms_interface b,host d,
							(select count(*) as num, a.interface_id from plugin_nms_log a,plugin_nms_interface b where $where1 a.interface_id =b.interface_id and nms_time BETWEEN '$datefrom' and '$dateto' GROUP BY a.interface_id ORDER BY num DESC $where) c
							Where a.interface_id = b.interface_id and a.interface_id = c.interface_id and b.host_id = d.id and a.nms_time BETWEEN '$datefrom' and '$dateto' GROUP BY log_id having num >='$limit' ORDER BY num DESC ,host_id,interface_name ,nms_time DESC");
					foreach ($data_report_interface as $set){
						$interface_name =  '"'.$set['interface_name'].'"';
						print $set['host_name'] . ",";
						print $interface_name . ",";
						print $set['nms_status'] . ",";
						print $set['nms_time'] . ",";
						print $set['user_active'] . "\n";
					}
					include_once(dirname(__FILE__) . "/model/nms_report.php");
					delete_host_report("interface_report");
					delete_group_report("interface_report");
					exit;
					}		
				}	
			}
			include_once (dirname(__FILE__) ."/nms_alarm_report_view.php");
			include_once (dirname(__FILE__) . "/libs/general_header.php");
			if($check_alarm==true){
				nms_alarm_report_draw();
			}else{
				include_once (dirname(__FILE__) . "/libs/auth.php");
			}				
			include_once($config['base_path'] . "/include/bottom_footer.php");
			break;
	/*
	 * Functon report module , port,transicver
	 * */
	case 'report_device' :
				if(isset($_REQUEST['export'])){
					/*
					 * Report transicver
					 * */
					if($_REQUEST['export']=="trans"){
						if(empty($_REQUEST['htrans'])){
							echo'<script>
								alert("Please choose device !!!");
								</script>	';
						}elseif (empty($_REQUEST['strans'])){
							echo'<script>
								alert("Please choose group interface !!!");
								</script>	';
						}elseif (empty($_REQUEST['fil'])){
							echo'<script>
								alert("Please choose filter  !!!");
								</script>	';
						}elseif (empty($_REQUEST['th'])){
							echo'<script>
								alert("Please choose value report  !!!");
								</script>	';
						}else{
						$date_from_trans =$_REQUEST['datefrom'];
						$date_to_trans=$_REQUEST['dateto'];
						$where ='';
						if($_REQUEST['fil']==1){
							switch ($_REQUEST['th']){
								case '1': $where =" and a.thold =1 ";
								break;
								case '2': $where =" and (a.thold =2) or (a.thold =1)";
								break;
								case '3': $where =" and (a.thold =3) or (a.thold =4)";
								break;
								case '4': $where =" and a.thold =4";
								break;		
							}
						}elseif ($_REQUEST['fil']==2){
							switch ($_REQUEST['th']){
								case '1': $where =" and a.rvalue <=-10";
								break;
								case '2': $where =" and a.rvalue <=-15";
								break;
								case '3': $where =" and a.rvalue <=-20";
								break;
								case '4': $where =" and a.rvalue <=-25";
								break;
								case '5': $where =" and a.rvalue <=-30";
								break;
								case '6': $where =" and a.rvalue <=-35";
								break;
								case '7': $where =" and a.rvalue <=-40";
								break;
							}
						}elseif ($_REQUEST['fil']==3){
							$limit = $_REQUEST['th'];
								$where =" and a.rvalue <= $limit";
						}elseif ($_REQUEST['fil']==4){
							$where="";
						}
						header("Content-type: application/csv");
						header("Content-Disposition: attachment; filename=transciever_results.csv");
						print "Host_name,Module,Interface,Slot,Rvalue,Thold,Transceiver_name,Transceiver_serial,Date\n";
						if($_REQUEST['htrans']=='all'&& $_REQUEST['strans']=='all_group'){
								 $result_trans = db_fetch_assoc("Select CONCAT_WS('',d.description,' (',d.hostname,')') as host_name,CONCAT('(',a.port_name,')','\t',a.port_alias) as interface , a.port_name as port_name
																	,a.rvalue as rvalue ,a.trans_date as trans_date ,CONCAT_WS('',b.module_name,' (',b.module_serial,')')as module_name , a.thold as thold ,trans_name,trans_serial  
																	FROM plugin_nms_translog a, plugin_nms_module b,host d
																	WHERE a.module_serial =b.module_serial and a.host_id =d.id and a.trans_date >='$date_from_trans' and a.trans_date <='$date_to_trans'".$where."
																	order by host_id,b.module_serial,trans_date");
									
								foreach ($result_trans as $set){
									switch ($set['thold']){
										case '1': $thold = '++';
										break;
										case '2': $thold = '+';
										break;
										case '3': $thold = '-';
										break;
										case '4': $thold = '--';
										break;
										case '0': $thold = '';
										break;
									
									}
									$interface_name =  '"'.$set['interface'].'"';
									$port_trans = substr($set['port_name'], 2,1);
									print $set['host_name'] . ",";
									print $set['module_name'] . ",";
									print $interface_name . ",";
									print $port_trans .",";
									print $set['rvalue'] . ",";
									print $thold . ",";
									print $set['trans_name'] . ",";
									print $set['trans_serial'] . ",";
									print $set['trans_date'] . "\n";
								}
						}elseif ($_REQUEST['htrans']=='select'&& $_REQUEST['strans']=='all_group'){
							$result_host = db_fetch_assoc('SELECT host_id FROM plugin_nms_report_host where report_group ="transeiver_report" group by host_id');
							foreach ($result_host as $item){
								$result_trans = db_fetch_assoc("Select CONCAT_WS('',d.description,' (',d.hostname,')') as host_name,CONCAT('(',a.port_name,')','\t',a.port_alias) as interface , a.port_name as port_name
										,a.rvalue as rvalue ,a.trans_date as trans_date ,CONCAT_WS('',b.module_name,' (',b.module_serial,')')as module_name , a.thold as thold ,trans_name,trans_serial
										FROM plugin_nms_translog a, plugin_nms_module b,host d
										WHERE a.module_serial =b.module_serial and a.host_id =d.id and a.trans_date >='$date_from_trans' and a.trans_date <='$date_to_trans'".$where." and a.host_id = ".$item['host_id']."
																	order by host_id,b.module_serial,trans_date");
									
								foreach ($result_trans as $set){
									switch ($set['thold']){
										case '1': $thold = '++';
										break;
										case '2': $thold = '+';
										break;
										case '3': $thold = '-';
										break;
										case '4': $thold = '--';
										break;
										case '0': $thold = '';
										break;
											
									}
									$interface_name =  '"'.$set['interface'].'"';
									$port_trans = substr($set['port_name'], 2,1);
									print $set['host_name'] . ",";
									print $set['module_name'] . ",";
									print $interface_name . ",";
									print $port_trans .",";
									print $set['rvalue'] . ",";
									print $thold . ",";
									print $set['trans_name'] . ",";
									print $set['trans_serial'] . ",";
									print $set['trans_date'] . "\n";
								}
							}
						
						}elseif($_REQUEST['htrans']=='all'&& $_REQUEST['strans']=='select_group'){
							$result_group = db_fetch_assoc('SELECT interface_group_id FROM plugin_nms_report_group where report_group= "transeiver_report" GROUP BY interface_group_id');
							foreach ($result_group as $item){
							$result_trans = db_fetch_assoc("Select CONCAT_WS('',d.description,' (',d.hostname,')') as host_name,CONCAT('(',a.port_name,')','\t',a.port_alias) as interface , a.port_name as port_name
										,a.rvalue as rvalue ,a.trans_date as trans_date ,CONCAT_WS('',b.module_name,' (',b.module_serial,')')as module_name , a.thold as thold ,trans_name,trans_serial
										FROM plugin_nms_translog a, plugin_nms_module b,host d
										WHERE a.module_serial =b.module_serial and a.host_id =d.id and a.trans_date >='$date_from_trans' and a.trans_date <='$date_to_trans'".$where." and a.interface_group_id = ".$item['interface_group_id']."
																	order by host_id,b.module_serial,trans_date");
									
								foreach ($result_trans as $set){
									switch ($set['thold']){
										case '1': $thold = '++';
										break;
										case '2': $thold = '+';
										break;
										case '3': $thold = '-';
										break;
										case '4': $thold = '--';
										break;
										case '0': $thold = '';
										break;
											
									}
									$interface_name =  '"'.$set['interface'].'"';
									$port_trans = substr($set['port_name'], 2,1);
									print $set['host_name'] . ",";
									print $set['module_name'] . ",";
									print $interface_name . ",";
									print $port_trans .",";
									print $set['rvalue'] . ",";
									print $thold . ",";
									print $set['trans_name'] . ",";
									print $set['trans_serial'] . ",";
									print $set['trans_date'] . "\n";
								}
							}
						}elseif ($_REQUEST['htrans']=='select'&& $_REQUEST['strans']=='select_group'){
							$result_host_group = db_fetch_assoc('SELECT host_id , interface_group_id FROM plugin_nms_report_host a ,plugin_nms_report_group b where a.report_group ="transeiver_report" and a.report_group =b.report_group group by host_id,interface_group_id');
							foreach ($result_host_group as $item){
							$result_trans = db_fetch_assoc("Select CONCAT_WS('',d.description,' (',d.hostname,')') as host_name,CONCAT('(',a.port_name,')','\t',a.port_alias) as interface , a.port_name as port_name
										,a.rvalue as rvalue ,a.trans_date as trans_date ,CONCAT_WS('',b.module_name,' (',b.module_serial,')')as module_name , a.thold as thold ,trans_name,trans_serial
										FROM plugin_nms_translog a, plugin_nms_module b,host d
										WHERE a.module_serial =b.module_serial and a.host_id =d.id and a.trans_date >='$date_from_trans' and a.trans_date <='$date_to_trans'".$where." and a.host_id = ".$item['host_id']." and a.interface_group_id = ".$item['interface_group_id']."
																	order by host_id,b.module_serial,trans_date");
								foreach ($result_trans as $set){
									switch ($set['thold']){
										case '1': $thold = '++';
										break;
										case '2': $thold = '+';
										break;
										case '3': $thold = '-';
										break;
										case '4': $thold = '--';
										break;
										case '0': $thold = '';
										break;
											
									}
									$interface_name =  '"'.$set['interface'].'"';
									$port_trans = substr($set['port_name'], 2,1);
									print $set['host_name'] . ",";
									print $set['module_name'] . ",";
									print $interface_name . ",";
									print $port_trans .",";
									print $set['rvalue'] . ",";
									print $thold . ",";
									print $set['trans_name'] . ",";
									print $set['trans_serial'] . ",";
									print $set['trans_date'] . "\n";
								}
							}
						}
						include_once(dirname(__FILE__) . "/model/nms_report.php");
						delete_host_report("transeiver_report");
						delete_group_report("transeiver_report");
						exit;
						}
					}
				}
				/*
				 * Report port of devices
				 * */
				if(isset($_REQUEST['export'])){
					if($_REQUEST['export']=="port"){
						if(empty($_REQUEST['port'])){
							echo'<script>
								alert("Please choose port !!!");
								</script>	';
						}elseif (empty($_REQUEST['hport'])){
							echo'<script>
								alert("Please choose devices !!!");
								</script>	';
						}else{
						$date_from_port =$_REQUEST['datefrom'];
						$date_to_port=$_REQUEST['dateto'];
						$port =$_REQUEST['port'];
						header("Content-type: application/csv");
						header("Content-Disposition: attachment; filename=trans_port_report.csv");
						print "Host_name,Interface,Slot,Port,Module_name,Module_serial,Transceiver_name,Transceiver_serial,Status,Date,Interface Group\n";
						if($_REQUEST['hport']=='all'){
								$result_module = db_fetch_assoc("SELECT a.port_name as port_name,a.port_status as status ,a.trans_date as trans_date ,c.module_name as module_name,a.module_serial as module_serial
																,a.trans_serial as trans_serial, a.trans_name as trans_name,d.group_name as group_name,
															CONCAT('(',a.port_name,')','\t',a.port_alias) as interface_name ,CONCAT_WS('',b.description,' (',b.hostname,')') as host_name 
															FROM plugin_nms_translog a,host b ,plugin_nms_module c, plugin_nms_groupname d  WHERE 
															a.host_id =b.id and a.module_serial =c.module_serial and a.port_name like '%".$port."%' and a.trans_date >='$date_from_port' and a.trans_date <='$date_to_port' and a.interface_group_id = d.interface_group_id
															 ORDER BY  a.host_id,Convert(SUBSTRING(a.port_name,4,2),UNSIGNED INTEGER),a.trans_date DESC");
							foreach ($result_module as $item){
								$interface_name =  '"'.$item['interface_name'].'"';
								$tran_num = substr($item['port_name'], 2,1);
								$test = substr($item['port_name'], 4,2);
								print $item['host_name'] . ",";
								print $interface_name . ",";
								print $tran_num . ",";
								print $test . ",";
								print $item['module_name'] . ",";
								print $item['module_serial'] . ",";
								print $item['trans_name'] . ",";
								print $item['trans_serial'] . ",";
								print $item['status'] . ",";
								print $item['trans_date'] . ",";
								print $item['group_name']."\n";
							}
						}elseif ($_REQUEST['hport']=='select'){
							$host_module = db_fetch_assoc('SELECT host_id FROM plugin_nms_report_host where report_group ="port_report"');
							foreach ($host_module as $host){
							$result_module = db_fetch_assoc("SELECT a.port_name as port_name,a.port_status as status ,a.trans_date as trans_date ,c.module_name as module_name,a.module_serial as module_serial
															,a.trans_serial as trans_serial, a.trans_name as trans_name, d.group_name as group_name,
														CONCAT('(',a.port_name,')','\t',a.port_alias) as interface_name ,CONCAT_WS('',b.description,' (',b.hostname,')') as host_name 
														FROM plugin_nms_translog a,host b ,plugin_nms_module c,plugin_nms_groupname d  WHERE a.host_id =".$host['host_id']." and
														a.host_id =b.id and a.module_serial =c.module_serial and a.port_name like '%".$port."%' and a.trans_date >='$date_from_port' and a.trans_date <='$date_to_port'and a.interface_group_id = d.interface_group_id
														 ORDER BY  a.host_id, Convert(SUBSTRING(a.port_name,4,2),UNSIGNED INTEGER),a.trans_date DESC");
							foreach ($result_module as $item){
								$interface_name =  '"'.$item['interface_name'].'"';
								$tran_num = substr($item['port_name'], 2,1);
								$test = substr($item['port_name'], 4,2);
								print $item['host_name'] . ",";
								print $interface_name . ",";
								print $tran_num . ",";
								print $test . ",";
								print $item['module_name'] . ",";
								print $item['module_serial'] . ",";
								print $item['trans_name'] . ",";
								print $item['trans_serial'] . ",";
								print $item['status'] . ",";
								print $item['trans_date'] . ",";
								print $item['group_name']."\n";
							}
						}
						}
						include_once(dirname(__FILE__) . "/model/nms_report.php");
						delete_host_report("port_report");
						exit;
						}
					}
				}
				
				/*
				 * Report module (Card) of Devices
				 *   */
				if(isset($_REQUEST['export'])){
					if($_REQUEST['export']=="module"){
						if(empty($_REQUEST['module']) or $_REQUEST['module']=='null'){
							echo'<script>
								alert("Please choose module !!!");
								</script>	';
						}elseif (empty($_REQUEST['hmodule'])){
							echo'<script>
								alert("Please choose devices !!!");
								</script>	';
						}else{
						$date_from_module =$_REQUEST['datefrom'];
						$date_to_module=$_REQUEST['dateto'];
						$module =$_REQUEST['module'];
						$module = str_replace('*', '+',$module);
						header("Content-type: application/csv");
						header("Content-Disposition: attachment; filename=module_report.csv");
						print "Host_name,Module,Slot_case,Serial,Hardware,Firmware,Software,Module_date,Total_port_used,Total_port_not_used,Total_port_unused\n";
						if($_REQUEST['hmodule']=='all'){
							if($module =='all'){
								$host_module = db_fetch_assoc('SELECT host_id FROM plugin_nms_interface group by host_id');
								foreach ($host_module as $host){
								$card = db_fetch_assoc("select module_name from plugin_nms_module group by module_name order by module_name ASC");
									foreach ($card as $list){
									$result_module = db_fetch_assoc("SELECT a.module_name as module_name  ,a.module_serial as module_serial ,b.module_num as module_num ,b.module_date as module_date ,
										b.software as software ,a.firmware as firmware ,a.hardware as hardware ,CONCAT_WS('',c.description,' (',c.hostname,')') as host_name
										FROM plugin_nms_module a, plugin_nms_modulelog b ,host c WHERE b.host_id = ".$host['host_id']." and
											a.module_serial =b.module_serial and a.module_name ='".$list['module_name']."' and b.module_date >= '$date_from_module' and b.module_date <= '$date_to_module'
											and b.host_id = c.id ORDER BY b.host_id,  b.module_num ASC , a.module_name, a.module_serial , b.module_date DESC");

											foreach ($result_module as $item){
												include_once(dirname(__FILE__) . "/model/nms_dim_model.php");
												$port_used = "";
												//get_sum_port_used($host['host_id'], $item['module_date'],$item['module_num']);
												$port_empty=""; 
												//get_sum_port_empty($host['host_id'], $item['module_date'],$item['module_num']);
												$port_notused ="";
												// get_sum_port_notuse($host['host_id'], $item['module_date'],$item['module_num']);
												print $item['host_name'] . ",";
												print $item['module_name'] . ",";
												print $item['module_num'] . ",";
												print $item['module_serial'] . ",";
												print $item['hardware'] . ",";
												print $item['firmware'] . ",";
												print $item['software'] . ",";
												print $item['module_date']. ",";
												print $port_used . ",";
												print $port_empty . ",";
												print $port_notused . "\n";
											
										}
									}
								}
							}
							else{
							$host_module = db_fetch_assoc('SELECT host_id FROM plugin_nms_interface group by host_id');
							foreach ($host_module as $host){
								$result_module = db_fetch_assoc("SELECT a.module_name as module_name  ,a.module_serial as module_serial ,b.module_num as module_num ,b.module_date as module_date ,
										b.software as software ,a.firmware as firmware ,a.hardware as hardware ,CONCAT_WS('',c.description,' (',c.hostname,')') as host_name
										FROM plugin_nms_module a, plugin_nms_modulelog b ,host c WHERE b.host_id = ".$host['host_id']." and
										a.module_serial =b.module_serial and a.module_name ='$module' and b.module_date >= '$date_from_module' and b.module_date <= '$date_to_module'
										and b.host_id = c.id ORDER BY  b.host_id,  b.module_num ASC , a.module_name, a.module_serial , b.module_date DESC");
										foreach ($result_module as $item){
											include_once(dirname(__FILE__) . "/model/nms_dim_model.php");
											$port_used = "";
											//get_sum_port_used($host['host_id'], $item['module_date'],$item['module_num']);
											$port_empty= "";
											//get_sum_port_empty($host['host_id'], $item['module_date'],$item['module_num']);
											$port_notused = "";
											//get_sum_port_notuse($host['host_id'], $item['module_date'],$item['module_num']);
											print $item['host_name'] . ",";
											print $item['module_name'] . ",";
											print $item['module_num'] . ",";
											print $item['module_serial'] . ",";
											print $item['hardware'] . ",";
											print $item['firmware'] . ",";
											print $item['software'] . ",";
											print $item['module_date'] . ",";
											print $port_used . ",";
											print $port_empty . ",";
											print $port_notused . "\n";
											
										}
							}
							}
						}elseif ($_REQUEST['hmodule']=='select'){
							if($module =='all'){
								$card = db_fetch_assoc("select module_name from plugin_nms_module group by module_name order by module_name ASC");
								foreach ($card as $list){
								$host_module = db_fetch_assoc('SELECT host_id FROM plugin_nms_report_host where report_group ="module_report"');
								foreach ($host_module as $host){
									$result_module = db_fetch_assoc("SELECT a.module_name as module_name  ,a.module_serial as module_serial ,b.module_num as module_num ,b.module_date as module_date ,
										b.software as software ,a.firmware as firmware ,a.hardware as hardware ,CONCAT_WS('',c.description,' (',c.hostname,')') as host_name
										FROM plugin_nms_module a, plugin_nms_modulelog b ,host c WHERE b.host_id = ".$host['host_id']." and
											a.module_serial =b.module_serial and a.module_name ='".$list['module_name']."' and b.module_date >= '$date_from_module' and b.module_date <= '$date_to_module'
											and b.host_id = c.id ORDER BY b.host_id,  b.module_num ASC , a.module_name, a.module_serial , b.module_date DESC");
											foreach ($result_module as $item){
												include_once(dirname(__FILE__) . "/model/nms_dim_model.php");
												$port_used = 
												//get_sum_port_used($host['host_id'], $item['module_date'],$item['module_num']);
												$port_empty= 
												//get_sum_port_empty($host['host_id'], $item['module_date'],$item['module_num']);
												$port_notused =
												// get_sum_port_notuse($host['host_id'], $item['module_date'],$item['module_num']);
												print $item['host_name'] . ",";
												print $item['module_name'] . ",";
												print $item['module_num'] . ",";
												print $item['module_serial'] . ",";
												print $item['hardware'] . ",";
												print $item['firmware'] . ",";
												print $item['software'] . ",";
												print $item['module_date']  .",";
												print $port_used . ",";
												print $port_empty . ",";
												print $port_notused . "\n";
										}
									}
								}
							}else{
							$host_module = db_fetch_assoc('SELECT host_id FROM plugin_nms_report_host where report_group ="module_report"');
							foreach ($host_module as $host){
								$result_module = db_fetch_assoc("SELECT a.module_name as module_name  ,a.module_serial as module_serial ,b.module_num as module_num ,b.module_date as module_date ,
										b.software as software ,a.firmware as firmware ,a.hardware as hardware ,CONCAT_WS('',c.description,' (',c.hostname,')') as host_name
										FROM plugin_nms_module a, plugin_nms_modulelog b ,host c WHERE b.host_id = ".$host['host_id']." and
										a.module_serial =b.module_serial and a.module_name ='$module' and b.module_date >= '$date_from_module' and b.module_date <= '$date_to_module'
										and b.host_id = c.id ORDER BY b.host_id,  b.module_num ASC , a.module_name, a.module_serial , b.module_date DESC");
										foreach ($result_module as $item){
											include_once(dirname(__FILE__) . "/model/nms_dim_model.php");
											$port_used = get_sum_port_used($host['host_id'], $item['module_date'],$item['module_num']);
											$port_empty= get_sum_port_empty($host['host_id'], $item['module_date'],$item['module_num']);
											$port_notused = get_sum_port_notuse($host['host_id'], $item['module_date'],$item['module_num']);
											print $item['host_name'] . ",";
											print $item['module_name'] . ",";
											print $item['module_num'] . ",";
											print $item['module_serial'] . ",";
											print $item['hardware'] . ",";
											print $item['firmware'] . ",";
											print $item['software'] . ",";
											print $item['module_date'] . ",";
											print $port_used . ",";
											print $port_empty . ",";
											print $port_notused . "\n";
										}
								}
							}
						}
						include_once(dirname(__FILE__) . "/model/nms_report.php");
						delete_host_report("module_report");
						exit;
						}
					}
				}
				include_once (dirname(__FILE__) . "/libs/general_header.php");
				if($check_alarm==true){
					include_once (dirname(__FILE__) . "/nms_trans_report_view.php");
					include_once (dirname(__FILE__) . "/nms_module_report_view.php");
					include_once (dirname(__FILE__) . "/nms_port_report_view.php");
					include_once (dirname(__FILE__) . "/controller/nms_report_controller.php");
					draw_report_device();
				}else{
					include_once (dirname(__FILE__) . "/libs/auth.php");
				}
				include_once($config['base_path'] . "/include/bottom_footer.php");
				break;
	default :
			include_once(dirname(__FILE__) . "/libs/general_header.php");
			include_once($config['base_path'] . "/include/bottom_footer.php");
			break;
	}

	
?>
